/**
*	Author:Chihi
*	Date: 2019/05/16
*	定制化组件,全局组件
**/
let maintencePopup = '';
let currentTestData = [];
let downInterval_delay = 0;
let downInterval_popup = 0;
let downInterval = 0;
let downInterval_pop = 0;
let downInterval_task = 0;
let isHasTest = false;
let isShowPop = false;
let notShow = false;//全局变量定义当前离线维保页面不要维保助手
let custom = (function(_self) {

	
	let currentTestEndTime = '';
	let maintenContainer = ".ilink-view-main";


	// 清除定时任务
	_self.clearAllInter = function(){
		if(downInterval){
			clearInterval(downInterval);
			downInterval = 0;
		}
		if(downInterval_delay){
			downInterval_delay = 0;
		}
		if(downInterval_popup){
			clearInterval(downInterval_popup);
			downInterval_popup = 0;
		}
		if (downInterval_pop) {
			clearInterval(downInterval_pop);
			downInterval_pop = 0;
		}
		if (downInterval_task) {
			clearInterval(downInterval_task);
			downInterval_task = 0;
		}
	}

	// 维保助手
	// type 1：测试类 2：保养类
	//
	_self.startMaintenancePlan = function() {
		_self.getTestTask();
	}

	// 是否有测试任务
	_self.getTestTask = function(){
		currentTestData = [];
		_self.clearAllInter();
		jQuery(".taskCountDownTimeTip").remove();
		Dao.getTestTasksByAccount({
			account : userInfor.accountName,
		}, function(data,total) {
			currentTestData = data;
			if(data && data.length > 0) {
			 	// 保养 维保
			 	if(data[0].taskType == 1 || data[0].taskType == 2){
			 		isHasTest = false;
			 	}else{
			 		isHasTest = true;
			 	}
			 	_self.showHelperEnter();
			}
		},false);
	}

	// 展示助手入口
	// flag 1:
	_self.showHelperEnter = function(){
		jQuery(".taskCountDownTimeTip").remove();
		if(isHasTest){
			let time = '0';
			$(maintenContainer).append(`
				<div class="taskCountDownTimeTip" id="taskCountDownTimeTip">
					<div class="time-Alarm">
				        <a href="#" id="task_timer_`+currentTestData[0].id+`">
				           <i class="iconfont icon-naozhong" style="font-size: 12px;margin-right:3px"></i>
				           <span id="contTime">`+time+`</span>
				       </a>
				    </div>
			    </div>
			  `);
			// 开启定时任务
			for(var i = 0;i<currentTestData.length;i++){
				let datatime = custom.showHalfTime(currentTestData[i].endTime).split(":")
				if(currentTestData[i].taskType==3 || currentTestData[i].taskType ==4){
					if(datatime[0]!="00" || datatime[1]!="00" || datatime[2]!="00"){
						currentTestEndTime = currentTestData[i].endTime;
						_self.startCountdown();
						break;
					}
					else{
						isHasTest = false;
						app.methods.showToastCenter(currentTestData[i].name+"已到期自动关闭")
						_self.getTestTask();
						break;
					}
				}else{
					app.methods.showToastCenter(currentTestData[i].name+"已到期自动关闭")
					_self.getTestTask();
				}

			}
		}else{
			$(maintenContainer).append(`
				<div class="taskCountDownTimeTip" id="taskCountDownTimeTip" style="background: #4CA4FE;">
					<div class="time-Alarm">
				        <a href="#">
				           <i class="iconfont icon-weixiuon" style="font-size: 12px;margin-right:3px"></i>维保助手
				       </a>
				    </div>
			    </div>
			  `);
		}
		jQuery(".time-Alarm").off("click").on("click",{'data':[]},function(e) {
			_self.openMaintencePopup();
		});
	}

	_self.openMaintencePopup = function(){
		maintencePopup = app.popup.create({
		  content: `<div class="popup maintence-popup">
		       <div class="title">
		       		<div class='top'>
		       			<span ><i class="iconfont icon-weixiuon " style="font-size: 14px;margin-right:3px;"></i>维保助手提醒您</span>
		       			<i class="iconfont icon-APPicon_guanbi close-maintencePopup " style="font-size: 12px;float: right;padding:10px;margin:-10px"></i>
		       		</div>
		       		<div class='bto'>你当前正在进行<span id="maintence_total">`+currentTestData.length+`</span>项维保任务</div>
		       </div>
		       <div class="page-content cotent">
		       		<div class="delay-task-list">
						<ul id="delayTaskList_list"></ul>
						<div class="end-all-task">全部结束</div>
					</div>
		       </div>
		  </div>`,
		  on: {
		    open: function (popup) {
		      	
		    },
		    opened: function (popup) {
		    	_self.showTaskList();
		    	// 全部结束
		    	jQuery(".end-all-task").off().click(function(){
		    		// 全部任务id
		    		let allId = [];
		    		for(let i=0;i<currentTestData.length;i++){
		    			allId.push(currentTestData[i].id);
		    		}
		    		confimEndTest("",function(){
		    			common.loading(1);
		    			Dao.endTasksByIds({ids:allId.toString()},function(data){
		    				setTimeout(function(){
		    					app.methods.showToastCenter("操作成功");
		    					_self.showTaskList();
		    					_self.getTestTask();
								_self.refreshTaskList();
								if(app.view.current.router.currentPageEl.f7Component.pageName=='allBuild'){
									app.view.current.router.currentPageEl.f7Component.getTestTaskStatus()
								}
		    					common.loading(0);
		    				},3000);
		    			},false);
		    		});
		    	});
		    },
		  }
		});
		maintencePopup.open();
		jQuery(".close-maintencePopup").off().click(function(){
			maintencePopup.close();
		});
	}

	// 
	_self.showTaskList = function(){
		common.loading(1);
		jQuery("#delayTaskList_list").empty();
		currentTestData = [];
		Dao.getTestTasksByAccount({
			account : userInfor.accountName,
		}, function(data,total) {
			common.loading(0);
			if(data && data.length > 0) {
				if(data.length == 1){
					jQuery(".end-all-task").hide();
				}else{
					jQuery(".end-all-task").show();
				}
			 	currentTestData = data;
				for (var i = 0; i < data.length; i++) {
					var arrayData = {
							"id":data[i].taskId,
							"type": data[i].taskType,
							"orgName":data[i].orgName,
							"status":1,
							"taskName": data[i].name,
							"orgId":data[i].orgId,
							"complete":data[i].complete,
							"total":data[i].total
					}
					
					// 保养，维保巡查
					if(data[i].taskType == 1 || data[i].taskType == 2){
						$("#delayTaskList_list").append(`
							<li id="delayTaskList_list_`+data[i].id+`">
								<div class="each-list">
									<div class="delay-task-list-item-inner">
										<div class="item-title-row" id="task_`+data[i].id+`" style="font-weight:bold">`+data[i].name+`</div>
										<div class="item-subtitle">
											业主单位：<strong style="color:#333333">`+data[i].orgName+`</strong>
										</div>
									</div>
									<div class="delay-task-list-button end-time" data-id="`+data[i].id+`">
										<span class="right"> <a href="#" class="" >结束</a>
										</span>
									</div>
								</div>
							</li>
						`);
					}else{
						let delayTime = _self.showHalfTime(data[i].endTime);
						let delayTimeSplit = _self.showHalfTime(data[i].endTime).split(":");
						if (parseInt(delayTimeSplit[0]) >= 2) {
							$("#delayTaskList_list").append(`
								<li id="delayTaskList_list_`+data[i].id+`">
									<div class="each-list">
										<div class="delay-task-list-item-inner">
											<div class="delay-task-time over-one-hour" data-time="`+data[i].endTime+`">
												<span class="overtime h">`+delayTimeSplit[0]+`</span> <span class="overdot">:</span>
												<span class="overtime m">`+delayTimeSplit[1]+`</span> <span class="overdot">:</span>
												<span class="overtime s">`+delayTimeSplit[2]+`</span> <span class="prompt-title">后失效</span>
											</div>
											<div class="item-title-row" id="task_`+data[i].id+`">`+data[i].name+`</div>
											<div class="item-subtitle">
												业主单位：<strong style="color:#333333">`+data[i].orgName+`</strong>
											</div>
										</div>
										<div class="delay-task-list-button end-time" data-id="`+data[i].id+`">
											<span class="right"> <a href="#" class="" >结束</a>
											</span>
										</div>

									</div>
								</li>
							`);
						} else if (parseInt(delayTimeSplit[0]) < 2 && parseInt(delayTimeSplit[0]) >=1 ) {
							$("#delayTaskList_list").append(`
								<li id="delayTaskList_list_`+data[i].id+`">
									<div class="each-list">
										<div class="delay-task-list-item-inner">
											<div class="delay-task-time over-one-hour" data-time="`+data[i].endTime+`">
											  <span class="overtime h">`+delayTimeSplit[0]+`</span> <span class="overdot">:</span>
											  <span class="overtime m">`+delayTimeSplit[1]+`</span> <span class="overdot">:</span>
											  <span class="overtime s">`+delayTimeSplit[2]+`</span> <span class="prompt-title">后失效</span>
											</div>
											<div class="item-title-row" id="task_`+data[i].id+`" style="font-weight: bold;">`+data[i].name+`</div>
											<div class="item-subtitle">
												业主单位：<strong style="color:#333333">`+data[i].orgName+`</strong>
											</div>
										</div>
										<div class="delay-task-list-button-two">
											<div class="left end-time" data-id="`+data[i].id+`">
												<a href="#" style="color: #999999;" class="" >结束</a>
											</div>
											<div class="right delay-close" data-time="`+data[i].endTime+`" data-id="`+data[i].id+`">
												<a href="#" class="">延时2小时</a>
											</div>
										</div>
									</div>
								</li>
							`);
						} else if (parseInt(self.minute) > 10) {
							$("#delayTaskList_list").append(`
								<li id="delayTaskList_list_`+data[i].id+`">
									<div class="each-list">
										<div class="delay-task-list-item-inner">
											<div class="delay-task-time over-ten-min" data-time="`+data[i].endTime+`">
											<span class="overtime h">`+delayTimeSplit[0]+`</span> <span class="overdot">:</span>
											<span class="overtime m">`+delayTimeSplit[1]+`</span> <span class="overdot">:</span>
											<span class="overtime s">`+delayTimeSplit[2]+`</span> <span class="prompt-title">后失效</span>
											</div>
											<div class="item-title-row" id="task_`+data[i].id+`" style="font-weight: bold;">`+data[i].name+`</div>
											<div class="item-subtitle">
												业主单位：<strong style="color:#333333">`+data[i].orgName+`</strong>
											</div>
										</div>
										<div class="delay-task-list-button-two">
											<div class="delay-task-list-button-two">
												<div class="left end-time" data-id="`+data[i].id+`">
													<a href="#" style="color: #999999;" class="">结束</a>
												</div>
												<div class="right delay-close" data-time="`+data[i].endTime+`" data-id="`+data[i].id+`">
													<a href="#" class="">延时2小时</a>
												</div>
											</div>
										</div>
									</div>
								</li>
							`);
						} else {
							$("#delayTaskList_list").append(`
								<li id="delayTaskList_list_`+data[i].id+`">
									<div class="each-list">
										<div class="delay-task-list-item-inner">
											<div class="delay-task-time less-ten-min" data-time="`+data[i].endTime+`">
											<span class="overtime h">`+delayTimeSplit[0]+`</span> <span class="overdot">:</span>
											<span class="overtime m">`+delayTimeSplit[0]+`</span> <span class="overdot">:</span>
											<span class="overtime s">`+delayTimeSplit[0]+`</span> <span class="prompt-title">后失效</span>
											</div>
											<div class="item-title-row" id="task_`+data[i].id+`" style="font-weight: bold;">`+data[i].name+`</div>
											<div class="item-subtitle">
												业主单位：<strong style="color:#333333">`+data[i].orgName+`</strong>
											</div>
										</div>
										<div class="delay-task-list-button-two">
											<div class="left end-time" data-id="`+data[i].id+`">
												<a href="#" style="color: #999999;" class="">结束</a>
											</div>
											<div class="right delay-close" data-time="`+data[i].endTime+`" data-id="`+data[i].id+`">
												<a href="#" class="" >延时2小时</a>
											</div>
										</div>
									</div>
								</li>
							`);
						}
					}
					
					// 进入任务
					jQuery("#task_"+data[i].id).off().click({'data':arrayData},function(e){
						jQuery(".close-maintencePopup").click();
						var data = e.data.data;
						var taskType = data.type;
						if(taskType == 1) { // 巡查
							app.router.navigate("/allBuild/maintainInspect/"+data.id+"/"+data.orgName+"/"+encodeURIComponent(data.taskName)+"/"+data.status+"/"+data.orgId+"/");
						} // 保养 
						else if(taskType == 2) {
							app.router.navigate("/allBuild/maintain/"+data.id+"/"+data.orgName+"/"+encodeURIComponent(data.taskName)+"/"+data.status+"/"+data.orgId+"/");
						} //单点测试
						else if (taskType == 3) {
							app.router.navigate("/allBuild/singleTest/"+data.id+"/"+data.orgName+"/"+encodeURIComponent(data.taskName)+"/"+data.status+"/"+data.orgId+"/");
						} // 联动测试
						else {
							if (data.total == data.complete) { //已完成
								app.router.navigate("/linkTestDetail/"+data.id+"/");
							} else {
								app.router.navigate("/linkTestTask/"+data.id+"/");
							}
						}
					});

				}

				jQuery("#maintence_total").html(total);
				// 开启定时任务
				_self.startPopupCountdown();
					
				// 增加延时
				jQuery(".delay-close").off().click(function(){
					var endTime = $(this).attr("data-time");
					var id = $(this).attr("data-id");
					_self.delayRemind(endTime,id);
				});
				
				// 结束测试
				jQuery(".end-time").off().click(function(){
					var id = $(this).attr("data-id");
					isShowPop = true;
					confimEndTest(id,function(){
						setTimeout(function(){
							_self.getTestTask();
							_self.showTaskList();
							_self.refreshTaskList();
						},1000);
					});
				});
			}else{
				jQuery(".end-all-task").hide();
				jQuery("#maintence_total").html(0);
				jQuery("#delayTaskList_list").append(`<div class="no-data-img"><img src="`+listNoDataPic()+`"/></div>`)
			}
		},false);
	}
	
	// 维保助手里面结束任务后，如果当前是在列表页面（保养，维保巡查，单点测试，联动测试）要刷新当前列表
	_self.refreshTaskList = function(){
		let el = app.view.current.el.f7View.router.currentPageEl.f7Component;
	    if(el && el.pageName == "maintain"){
	    	el.closeTaskBackRefreshList()
	    }
	    else if(el && el.pageName == "linkTest"){
	    	el.closeTaskBackRefreshList()
	    }
	    else if(el && el.pageName == "singleTest"){
	    	el.closeTaskBackRefreshList()
	    }
	    else if(el && el.pageName == "maintainInspect"){
	    	el.closeTaskBackRefreshList()
	    }
	}
	

	// 延时提醒
	_self.delayRemind = function(time,id){
		// 隐藏原先列
		jQuery("#delayTaskList_list_"+id+" .each-list").slideUp(300);
		jQuery("#delayTaskList_list_"+id+" .delay-class").remove();
		let delayTimeSplit = _self.showHalfTime(time).split(":");
		let timeTwo = moment(time).add(2, 'h').format("YYYY-MM-DD HH:mm:ss");
		let delayTimeSplitTwo = _self.showHalfTime(timeTwo).split(":");
		let delayHtml = `
		<div class="delay-class delay-current-time" id="delay-con-`+id+`" data-old="`+time+`" data-new="`+timeTwo+`">
		  <div class="delay-class-body">
		    <div class="delay-class-body-hint">请确认是否延时2小时后提醒？</div>
		    <div class="delay-class-body-time">
		      <div class="prompt-class-top old-time">
		          <span class="overtime h" id="hour">`+delayTimeSplit[0]+`</span>
	              <span class="overdot">:</span>    
	              <span class="overtime m" id="minute">`+delayTimeSplit[1]+`</span>
	              <span class="overdot">:</span>
	              <span class="overtime s" id="second">`+delayTimeSplit[2]+`</span>
		      </div>
		      <div class="arrow-class-center">
		        	<span>+2</span>
		        	<div><i class="iconfont icon-single_arrow"></i></div>
		      </div>
		      <div class="prompt-class-top new-time">
	             <span class="overtime h" id="newHour">`+delayTimeSplitTwo[0]+`</span>
	             <span class="overdot">:</span>    
	             <span class="overtime m" id="newMinute">`+delayTimeSplitTwo[1]+`</span>
	             <span class="overdot">:</span>
	             <span class="overtime s" id="newSecond">`+delayTimeSplitTwo[2]+`</span>
		      </div>
		    </div>
		    <div class="prompt-class-button">
			  <div class="block">
		        <p class="row">
		        	<button class="col button button-big left" id="delay_cancel_`+id+`" data-id="`+id+`">取消</button>
		        	<button class="col button button-big right" id="delay_confim_`+id+`" data-id="`+id+`">确定延时</button>
		        </p>
			  </div>
			  </div>
		  </div>
		</div>`;

		jQuery("#delayTaskList_list_"+id).append(delayHtml);

		jQuery("#delay_cancel_"+id).off().click(function(event){
			let id = $(this).attr("data-id");
			jQuery("#delay-con-"+id).slideUp(300);
			jQuery("#delayTaskList_list_"+id+" .each-list").slideDown();
		});

		jQuery("#delay_confim_"+id).off().click(function(event){
			let id = $(this).attr("data-id");
			common.loading(1);
			Dao.delayTestTime({
				id: id,
			},function(data){
				// 删除延时提醒
				jQuery("#delayTaskList_list_"+id+" .delay-class").remove();
				jQuery("#delayTaskList_list_"+id).append(_self.tipSimple(1));
				common.loading(0);
				setTimeout(function(){
					jQuery("#delayTaskList_list_"+id+" .hint-layer").remove();
					_self.showTaskList();
					_self.getTestTask();
				},3000);
			},true);
		});

		_self.startDelayCountdown();
	}



	// 延时定时任务
	_self.startDelayCountdown = function(){
		clearInterval(downInterval_delay);
		downInterval_delay = setInterval(function () {
			$(".delay-current-time .old-time").removeClass('orange green yellow');

			let oldD = $(".delay-current-time").attr("data-old");
			let newD = $(".delay-current-time").attr("data-new");
			let oldD_delay = _self.showHalfTime(oldD).split(":");
			let newD_delay = _self.showHalfTime(newD).split(":");

			if(parseInt(oldD_delay[0]) > 0) {
				$(".delay-current-time .old-time").find(".overtime.h").html(oldD_delay[0]).addClass("green");
				$(".delay-current-time .old-time").find(".overtime.m").html(oldD_delay[1]).addClass("green");
				$(".delay-current-time .old-time").find(".overtime.s").html(oldD_delay[2]).addClass("green");

				$(".delay-current-time .new-time").find(".overtime.h").html(newD_delay[0]);
				$(".delay-current-time .new-time").find(".overtime.m").html(newD_delay[1]);
				$(".delay-current-time .new-time").find(".overtime.s").html(newD_delay[2]);
			} 
			else if (parseInt(oldD_delay[1])>9) {
				$(".delay-current-time .old-time").find(".overtime.h").html(oldD_delay[0]).addClass("yellow");
				$(".delay-current-time .old-time").find(".overtime.m").html(oldD_delay[1]).addClass("yellow");
				$(".delay-current-time .old-time").find(".overtime.s").html(oldD_delay[2]).addClass("yellow");

				$(".delay-current-time .new-time").find(".overtime.h").html(newD_delay[0]);
				$(".delay-current-time .new-time").find(".overtime.m").html(newD_delay[1]);
				$(".delay-current-time .new-time").find(".overtime.s").html(newD_delay[2]);
			}
			else if(parseInt(oldD_delay[2]) <= 9){
				$(".delay-current-time .old-time").find(".overtime.h").html(oldD_delay[0]).addClass("orange");
				$(".delay-current-time .old-time").find(".overtime.m").html(oldD_delay[1]).addClass("orange");
				$(".delay-current-time .old-time").find(".overtime.s").html(oldD_delay[2]).addClass("orange");

				$(".delay-current-time .new-time").find(".overtime.h").html(newD_delay[0]);
				$(".delay-current-time .new-time").find(".overtime.m").html(newD_delay[1]);
				$(".delay-current-time .new-time").find(".overtime.s").html(newD_delay[2]);
			}
		},1000);
	}

	// 弹窗定时任务
	_self.startPopupCountdown = function(){
		clearInterval(downInterval_popup);
		downInterval_popup = setInterval(function () {
			// 获取列表时间
			let timaArrEl = $(".delay-task-time");
			$(".delay-task-time .overtime").removeClass('orange green yellow');
			for(let i=0;i<timaArrEl.length;i++){
				let t = $(timaArrEl[i]).attr("data-time");
				let dt = _self.showHalfTime(t).split(":");
				if(parseInt(dt[0]) > 0) {
					$(timaArrEl[i]).find(".overtime.h").html(dt[0]).addClass("green");
					$(timaArrEl[i]).find(".overtime.m").html(dt[1]).addClass("green");
					$(timaArrEl[i]).find(".overtime.s").html(dt[2]).addClass("green");
					$(timaArrEl[i]).find(".prompt-title").addClass("green");
				} 
				else if (parseInt(dt[0]) <= 0 && parseInt(dt[1])>9) {
					$(timaArrEl[i]).find(".overtime.h").html(dt[0]).addClass("yellow");
					$(timaArrEl[i]).find(".overtime.m").html(dt[1]).addClass("yellow");
					$(timaArrEl[i]).find(".overtime.s").html(dt[2]).addClass("yellow");
					$(timaArrEl[i]).find(".prompt-title").addClass("yellow");
				}
				else if(parseInt(dt[1]) <= 9){
					$(timaArrEl[i]).find(".overtime.h").html(dt[0]).addClass("orange");
					$(timaArrEl[i]).find(".overtime.m").html(dt[1]).addClass("orange");
					$(timaArrEl[i]).find(".overtime.s").html(dt[2]).addClass("orange");
					$(timaArrEl[i]).find(".prompt-title").addClass("orange");
				}

				if((parseInt(dt[0]) == 0) && (parseInt(dt[1]) == 0) && (parseInt(dt[2]) == 0) ){
					clearInterval(downInterval_popup);
					downInterval_popup = 0;
					_self.showTaskList();
				}
			}
		},1000);
	}

	// 悬浮窗定时任务
	_self.startCountdown = function(){
		clearInterval(downInterval);
		downInterval = setInterval(function () {
			_self.showHelperEnter();
			$("#contTime").html(_self.showHalfTime());
			var time = _self.showHalfTime();
			var timeArr = time.split(":");
			if (!isShowPop) {
				if ( parseInt( timeArr[0])==0&&parseInt(timeArr[1])<=2 ) {
					isShowPop = true;
					// for(var i = 0;i<currentTestData.length;i++){
					// 	let hwy = custom.showHalfTime(currentTestData[i].endTime).split(":");
					// 	if(hwy[0]==="01" && hwy[1]==="12"&& hwy[2]==="00"){
					// 		threeMinPop(currentTestData[i]);
					// 	}
					// }
					if(native.getDataByKey("IS_OFFLINE_MODULE") != 1){
						threeMinPop(currentTestData[0]);
					}
			    } else if (parseInt( timeArr[0])==0&&parseInt(timeArr[1])==3&&parseInt(timeArr[1])==0) {
					isShowPop = true;
					if(native.getDataByKey("IS_OFFLINE_MODULE") != 1){
						threeMinPop(currentTestData[0]);
					}
			    }
			}
			if(notShow){$("#taskCountDownTimeTip").hide()}
		},1000);
	}

	// 返回剩余时间
	_self.showHalfTime = function(time){
		let now = moment().format("YYYY-MM-DD HH:mm:ss");
		let a = moment(time?time:currentTestEndTime);
		let b = moment(now); // 当前时间
		let tempTime = moment.duration(a-b);
		let h = tempTime.hours();
		let m = tempTime.minutes();
		let s = tempTime.seconds();
		let timeHour = "";  
		let timeMinute = "";
		let timeMsecond = "";
		
	    if (moment(time?time:currentTestEndTime).isAfter(common.transtimestamp(new Date()))) {
	    	timeHour = ((h<10)?("0"+h):h);
	    	timeMinute = ((m<10)?("0"+m):m);
	    	timeMsecond = ((s<10)?("0"+s):s);
	    }
	    // 超过剩余时间
	    else{  
	    	timeHour = "00";
	    	timeMinute = "00";
	    	timeMsecond = "00";
	    }	
        let remainingTimeHour = timeHour+":"+timeMinute+":"+timeMsecond;
        let remainingTimeMinute = "00:"+timeMinute+":"+timeMsecond;
        let remainingTimeSecond = "00:"+timeMinute+":"+timeMsecond;
		let newTime = '';
        if(!time){
        	$(".taskCountDownTimeTip").removeClass('orange green yellow');
        }
    	if(parseInt(timeHour) > 0) {
    		newTime = remainingTimeHour;
    		if(!time){
    			$(".taskCountDownTimeTip").addClass("green");
    		}
    	} 
    	else if (parseInt(timeHour) <= 0 && parseInt(timeMinute)>9) {
    		newTime = remainingTimeMinute;
    		if(!time){
    			$(".taskCountDownTimeTip").addClass("yellow");
    		}	
    	}
    	else if(parseInt(timeMinute) <= 9){
    		newTime = remainingTimeSecond;
    		if(!time){
    			$(".taskCountDownTimeTip").addClass("orange");
    		}
    	}
    	return newTime;
	}



	/**
	 * 无需操作的提示信息框 flag的值： 1—延时操作。2—结束测试. 3—扫码提示 4—倒计时提示
	 */
	_self.tipSimple = function(flag,info) {
		if(flag == 1) {  // 延时成功
			var html = `<div class="hint-layer">
		         <div class="hint-icon delay-icon"><i class="iconfont icon-duigou1"></i></div>
		         </div>`;
		} 
		
		if(flag == 2) { // 结束成功
			var html = `<div class="hint-layer">
		         <div class="hint-icon end-icon"><i class="iconfont icon-duigou1"></i></div>
		         <div class="hint-content prompt-content">结束成功</div>
		         </div>`;
		}
		
		if (flag == 3) {  // 扫码提示
			var html = `<div class="hint-layer">
		         <div class="hint-icon"><i class="iconfont icon-point"></i></div>
		         <div class="hint-content">`+info+`</div>
		         </div>`;
		}
		
		if (flag == 4) { // 2小时候再提醒
			var html = `<div class="hint-layer">
		         <div class="hint-icon end-icon"><i class="iconfont icon-naozhong-"></i></div>
		         <div class="hint-content prompt-content">2小时候再次提醒</div>
		         </div>`;
		}

		openLayer(html,(el)=>{})
	}


	return _self;
})(window.custom || {});

//维保模块点击是否添加任务提示
function confirmDetails(id,callback,type,dataId,dataType,detailId) {//id==tskId,callback==getTestTaskStatus(),type=0/2,detailId==详情id,dataType=详情type
	let html = `
	<div class="delay-class">
	  <div class="end-test-title"><span class="prompt-title">开始确认</span></div>
	  <div class="end-test-body">
		<div class="end-test-hint end-test-hint-style" id="allTaskTittle">是否开始任务？</div>
		<div class="prompt-class-button">
		<div class="block">
		  <p class="row">
		  <button class="col button button-big" id="cancel">否</button>
		  <button class="col button button-big button-fill" id="confirm">是</button>
		  </p>
		</div>
		</div>
	  </div>
	</div>`;
	openLayer(html,(el)=>{ 
		jQuery(".prompt-class-button #cancel").off().click(function(event){
			common.loading(0);
			layer.closeAll();
		});
		jQuery(".prompt-class-button #confirm").off().click(function(event){
			common.loading(1);
			if(type==0){
			Dao.startTestTask({//加入任务接口
				taskId: id,
				account: userInfor.accountName
			  }, function (data) {
				layer.closeAll();
				app.methods.showToastCenter("加入成功");
				custom.startMaintenancePlan();//生成维保助手
				// if(callback){
					let jumpFromPage=app.view.current.router.currentPageEl.f7Component.jumpFromPage
					//保养
					if(jumpFromPage == "maintainDetail" || jumpFromPage == "maintain") {
							app.router.navigate("/maintainTask/"+id+"/"+dataId+"/"+dataType+"/");
						}
						//单点测试详情
						else if(jumpFromPage == "singleTest" || jumpFromPage == "singleTestDetail") {
					
							app.router.navigate("/singleTestTask/"+id+"/"+dataId+"/"+dataType+"/"+detailId+"/");
						}
						//维保巡查列表/维保巡查详情
						else if(jumpFromPage == "maintainInspect" || jumpFromPage == "maintainInspectDetail"){
							app.router.navigate("/maintainInspectTask/"+id+"/"+dataId+"/"+dataType+"/");
						}
				//}
				common.loading(0);
			  }, false);
			}
			if(type==2){
				Dao.joinTestTask({
					id:id,
					account:userInfor.accountName
				},function(data){
					custom.startMaintenancePlan();
					app.methods.showToastCenter("加入成功");
					layer.closeAll();
					//保养
					if(jumpFromPage == "maintainDetail" || jumpFromPage == "maintain") {
						app.router.navigate("/maintainTask/"+id+"/"+dataId+"/"+dataType+"/");
					}
					//单点测试详情
					else if(jumpFromPage == "singleTest" || jumpFromPage == "singleTestDetail") {
				
						app.router.navigate("/singleTestTask/"+id+"/"+dataId+"/"+dataType+"/"+detailId+"/");
					}
					//维保巡查列表/维保巡查详情
					else if(jumpFromPage == "maintainInspect" || jumpFromPage == "maintainInspectDetail"){
						app.router.navigate("/maintainInspectTask/"+id+"/"+dataId+"/"+dataType+"/");
					}
					//if(callback) {
						
						//callback();

					//}
				},false);
			}
		})
	})
};
// 确认结束测试提示
function confimEndTest(id,callback) {
	let html = `
		<div class="delay-class">
		  <div class="end-test-title"><span class="prompt-title">结束确认</span></div>
		  <div class="end-test-body">
		    <div class="end-test-hint end-test-hint-style" id="allTaskTittle">确定结束本次作业吗？</div>
		    <div class="prompt-class-button">
  		  <div class="block">
  	        <p class="row">
  	        <button class="col button button-big" id="cancel">取消</button>
  	        <button class="col button button-big button-fill" id="confirm">确定</button>
  	        </p>
  		  </div>
  		  </div>
		  </div>
		</div>`;
	openLayer(html,(el)=>{ 
		jQuery(".prompt-class-button #cancel").off().click(function(event){
			common.loading(0);
			layer.closeAll();
		});
		if(id){
			jQuery(".prompt-class-button #confirm").off().click(function(event){
				common.loading(1);
			
				Dao.endTestTask({
					id:id,
				},function(data){
					custom.getTestTask();
					custom.tipSimple(2);
					layer.closeAll();
					if(callback){
						if(app.view.current.router.currentPageEl.f7Component&&app.view.current.router.currentPageEl.f7Component.pageName=='allBuild'){
							app.view.current.router.currentPageEl.f7Component.getTestTaskStatus()
						}
						callback();
					}
					common.loading(0);
				},false);
			});
		}else{
			$("#allTaskTittle").html("确定结束所有作业吗？");
			jQuery(".prompt-class-button #confirm").off().click(function(event){
				if(callback){
					callback();
					layer.closeAll();
				}
			});
		}
	});
}

// 开始测试提示
function startTest(id,btnBack) {
	var html = `
		 <div class="task-class-hint">
		   <div class="task-class-hint-top">
		     <span><i class="iconfont icon-laba"></i></span>
		     <span>请注意！</span>
		   </div>
		   <div class="task-class-hint-body">
		     <div class="hint-content-item">
		       <p class="hint-content-item-header">警情屏蔽</p>
		       <p class=""> 测试期间引发的设备报警都会不再主 动推送给监管人员。 </p>
		     </div>
		     <div class="hint-content-item">
		           <p class="hint-content-item-header">2小时</p>
		           <p class="">系统将会每2小时提醒您一次。 </p>
		         </div>
		         <div class="hint-content-item">
              <p class="hint-content-item-header">及时关闭</p>
              <p class="">作业结束后，请及时在APP上面关闭 右下角的测试提醒。  </p>
            </div>
		   </div>
		   <div class="prompt-class-button">
		     <div class="block">
		      <p class="row">
	             <button class="col button button-big button-fill" id="start">已了解，立即开始测试</button>
	          </p>
		     </div>
		   </div>
		 </div>
		`;
		
		openLayer(html,(el)=>{
			jQuery("#start").off().click(function(event) {
				/*
				 * if (btnBack) { btnBack(); }
				 */
				Dao.startTestTask({
					taskId:id,
					account:userInfor.accountName
				},function(data){
					app.methods.showToastCenter("加入成功");
					custom.startMaintenancePlan();
					if(btnBack) {
						layer.closeAll();
						btnBack();	
					}
				},true);
			});
		});
}
 
// 加入测试提示
function joinTest(staffNames,id,btnBack) {
	var html = `
  		 <div class="task-class-hint join-task-hint">
  		   <div class="task-class-hint-top">
  		     <span><i class="iconfont icon-laba"></i></span>
  		     <span>请注意！</span>
  		   </div>
  		   <div class="task-class-hint-body">
  		     <div class="hint-content-item">
  		       <p class="hint-content-item-header">当前测试人</p>
  		       <p class="">当前<strong>`+staffNames+`</strong>正在对本条测试任务进行作业。请确认自己是否加入 </p>
  		     </div>
  		     <div class="hint-content-item">
		           <p class="hint-content-item-header">警情屏蔽</p>
		           <p class=""> 测试期间引发的设备报警都会不再主 动推送给监管人员。 </p>
		         </div>
  		     <div class="hint-content-item">
		           <p class="hint-content-item-header">2小时</p>
		           <p class="">系统将会每2小时提醒您一次。 </p>
		         </div>
		         <div class="hint-content-item">
	               <p class="hint-content-item-header">及时关闭</p>
	               <p class="">作业结束后，请及时在APP上面关闭 右下角的测试提醒。  </p>
	             </div>
  		   </div>
  		   <div class="prompt-class-button">
   		     <div class="block">
   		      <p class="row">
   	             <button class="col button button-big button-fill" id="join">已了解，立即开始测试</button>
   	          </p>
   		     </div>
   		   </div>
  		 </div>
  		`;
  		
  	openLayer(html,(el)=>{ 
  		jQuery("#join").off().click(function(event) {
			Dao.joinTestTask({
				id:id,
				account:userInfor.accountName
			},function(data){
				custom.startMaintenancePlan();
				app.methods.showToastCenter("加入成功");
				if(btnBack) {
					layer.closeAll();
					btnBack();
				}
			},true);
		});
  	});
}

//倒计时3分钟之内的弹框
function threeMinPop(data) {
	clearInterval(downInterval_pop);
	var delayTime = custom.showHalfTime(data.endTime);
	let delayTimeSplit = custom.showHalfTime(data.endTime).split(":");
	let name = data.orgName;
	let html = `
	      <div class="only-delay-task" data-id="`+data.id+`"
	         data-time="`+data.endTime+`">
	              <div class="only-delay-task-inner">
	              <div class="only-delay-task-time less-three-min" data-time="`+data.endTime+`">
				    <span class="overtime h">`+delayTimeSplit[0]+`</span> <span class="overdot">:</span>
				    <span class="overtime m">`+delayTimeSplit[0]+`</span> <span class="overdot">:</span>
				    <span class="overtime s">`+delayTimeSplit[0]+`</span> <span class="prompt-title">后失效</span>
			     </div>
			    <div class="task-title-row" id="task_`+data.id+`">`+data.name+`</div>
				  <div class="task-subtitle">
				    业主单位：<strong style="color:#333333">`+data.orgName+`</strong>
				  </div>
			    </div>
			    <div class="delay-task-button-two">
				   <div class="left end-time" data-id="`+data.id+`">
					<a href="#" style="color: #999999;" class="">结束</a>
				   </div>
				   <div class="right add-delay" data-time="`+data.endTime+`" data-id="`+data.id+`">
					  <a href="#" class="" >延时2小时</a>
				   </div>
			    </div>
	           </div>`;
   openLayer(html,(el)=>{
   	clearInterval(downInterval_pop);
   	downInterval_pop = setInterval(function() {
   		let time = $(".only-delay-task-time").attr("data-time");
	    let time_delay = custom.showHalfTime(time).split(":");
	    $(".only-delay-task-time").find(".overtime.h").html(time_delay[0]);
	    $(".only-delay-task-time").find(".overtime.m").html(time_delay[1]);
	    $(".only-delay-task-time").find(".overtime.s").html(time_delay[2]);
	    if (time_delay.join(":") == '00:00:00') {
	    	clearInterval(downInterval_pop);
	        var id = jQuery(".only-delay-task").attr("data-id");
			currentTestEndTime = data.endTime;
	        //forceClose(name);
	        setTimeout(function() {
	        	custom.getTestTask();
	        	custom.showTaskList();
	        	custom.refreshTaskList();
	        },1000);
	    }
	},1000);

	// 增加延时
	jQuery(".add-delay").off().click(function(){
		var endTime = $(this).attr("data-time");
		var id = $(this).attr("data-id");
		delayRemind(endTime,id);
	});

	// 结束测试
	jQuery(".end-time").off().click(function(){
		var id = $(this).attr("data-id");
	    confimEndTest(id,function(){
	    setTimeout(function(){
	    	custom.getTestTask();
	    	custom.showTaskList();
	    	custom.refreshTaskList();
	    },1000);
	});
	});
  })
}

function delayRemind(time,id) {
	let delayTimeSplit = custom.showHalfTime(time).split(":");
	let timeTwo = moment(time).add(2, 'h').format("YYYY-MM-DD HH:mm:ss");
	let delayTimeSplitTwo = custom.showHalfTime(timeTwo).split(":");
	let html = `
		<div class="delay-class delay-current-time" id="delay-con-`+id+`" data-old="`+time+`" data-new="`+timeTwo+`">
		  <div class="delay-class-body" style="padding:0px">
		    <div class="delay-class-body-hint">请确认是否延时2小时后提醒？</div>
		    <div class="delay-class-body-time">
		      <div class="prompt-class-top old-time">
		          <span class="overtime h" id="hour">`+delayTimeSplit[0]+`</span>
	              <span class="overdot">:</span>    
	              <span class="overtime m" id="minute">`+delayTimeSplit[1]+`</span>
	              <span class="overdot">:</span>
	              <span class="overtime s" id="second">`+delayTimeSplit[2]+`</span>
		      </div>
		      <div class="arrow-class-center">
		        	<span>+2</span>
		        	<div><i class="iconfont icon-single_arrow"></i></div>
		      </div>
		      <div class="prompt-class-top new-time">
	             <span class="overtime h" id="newHour">`+delayTimeSplitTwo[0]+`</span>
	             <span class="overdot">:</span>    
	             <span class="overtime m" id="newMinute">`+delayTimeSplitTwo[1]+`</span>
	             <span class="overdot">:</span>
	             <span class="overtime s" id="newSecond">`+delayTimeSplitTwo[2]+`</span>
		      </div>
		    </div>
		    <div class="prompt-class-button">
			  <div class="block">
		        <p class="row">
		        	<button class="col button button-big left" id="delay_cancel_`+id+`" data-id="`+id+`">取消</button>
		        	<button class="col button button-big right" id="delay_confim_`+id+`" data-id="`+id+`">确定延时</button>
		        </p>
			  </div>
			  </div>
		  </div>
		</div>`;

		openLayer(html,(el)=>{
			jQuery("#delay_cancel_"+id).off().click(function(event){
				common.loading(0);
			    layer.closeAll();
			});

		    jQuery("#delay_confim_"+id).off().click(function(event){
			    let id = $(this).attr("data-id");
			    common.loading(1);
			    Dao.delayTestTime({
			    	id: id,
			    },function(data){
			        // 删除延时提醒
				    common.loading(0);
				    custom.tipSimple(1);
				    custom.getTestTask();
				    custom.showTaskList();
	          		custom.refreshTaskList();
				},true);
			});

			custom.startDelayCountdown();
		});
}

//强制关闭提示信息
function forceClose(name) {
	var html = `
	   	<div class="delay-class">
		  <div class="end-test-title"><span class="prompt-title">结束确认</span></div>
		  <div class="end-test-body">
		    <div class="end-test-hint end-test-hint-style" id="allTaskTittle">`+name+`已经强制关闭</div>
		    <div class="prompt-class-button">
  		  </div>
		  </div>
		</div>
	`;
	openLayer(html,(el)=>{});
}
